﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.DBLayer
{
     public class DBPatient
    {
         private SqlCommand dbCmd = null;
         
         public Patient FindPatient(string navn)
        {
            string sql = @"select nr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi,modul from patient " +
                           " where navn  = '" + navn; 

            Console.WriteLine("sql stmt = " + sql);

           dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            Patient patientObj;

            if (dbReader.Read())
            {
                try
                {
                    Console.WriteLine("antal felter " + dbReader.FieldCount);
                    patientObj = buildPatientObj(dbReader);
                   
                }
                catch (Exception e)
                {
                    Console.WriteLine("Patient objekt ikke fundet");
                    patientObj = null;
                }


            }

            else
            {
                patientObj = null;
            }
            dbReader.Close();
            DBConnection.Close();
            return patientObj;
        }


        public List<Patient> HentAllePatienter()
        {
            List<Patient> returnList = new List<Patient>();
            string sql = @"select patient.nr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi, modul, beskrivelse from patient, modul where patient.modul = modul.nr ";
                                  

            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);
        
            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

           Patient patientObj;

            while (dbReader.Read())
            {

                patientObj = buildPatientObj(dbReader);
                returnList.Add(patientObj);
            }
            DBConnection.Close();
            return returnList;

        }
        public List<Patient> HentAllePatienter(int modulnr)
        {
            List<Patient> returnList = new List<Patient>();
            string sql = @"select patient.nr, navn, adresse, postnr, byen, telefon, alder, dato,famorienteret, medicin, vaerdi, modul, beskrivelse from patient, modul where patient.modul = modul.nr and patient.modul = " + modulnr;


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            Patient patientObj;

            while (dbReader.Read())
            {

                patientObj = buildPatientObj(dbReader);
                returnList.Add(patientObj);
            }
            DBConnection.Close();
            return returnList;

        }
        private Patient buildPatientObj(IDataReader dbReader)
        {
            Patient patientObj = new Patient();
            try{
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                patientObj.Nr = Convert.ToInt32(dbReader["nr"].ToString());
                patientObj.Navn = dbReader["navn"].ToString();
                patientObj.Adresse = dbReader["adresse"].ToString();
                patientObj.Postnr = Convert.ToInt32(dbReader["postnr"].ToString());
                patientObj.Byen = dbReader["byen"].ToString();
                patientObj.Telefon = Convert.ToInt32(dbReader["telefon"].ToString());
                patientObj.Alder = Convert.ToInt32(dbReader["alder"].ToString());
                patientObj.Medicin = dbReader["medicin"].ToString();
                patientObj.Samtykke = dbReader["famorienteret"].ToString();
                patientObj.Vaerdi = dbReader["vaerdi"].ToString();
                patientObj.IndDato = Convert.ToDateTime(dbReader["dato"].ToString());
                Modul modulObj = new Modul();
                patientObj.Modul = modulObj;
                patientObj.Modul.Modulnr = Convert.ToInt32(dbReader["modul"].ToString());
                patientObj.Modul.Navn = dbReader["beskrivelse"].ToString();


            }
            catch(Exception ex)
            {

                Console.WriteLine("Patient objekt ikke oprettet korrekt");
                Console.WriteLine(ex);
                patientObj = null;
            }

            return patientObj;
        }
        public int InsertPatient(Patient patientObj)
        {
            int rc = -1;
            SqlCommand dbCmd = null;
            try
            { // tildel naeste nr
                String sql = "select max(nr) as nextnr from patient";
                Console.WriteLine("sql nex nr " + sql);
                SqlCommand dbCmd1 = DBConnection.GetDbCommand(sql);
                IDataReader dbReader;
                dbReader = dbCmd1.ExecuteReader();
                if (dbReader.Read())
                {
                    Console.WriteLine("næstenr : " );
                    if (dbReader["nextnr"] != null)
                   {

                    patientObj.Nr = Convert.ToInt32(dbReader["nextnr"].ToString()) + 1;
                    Console.WriteLine("næstenr : " + patientObj.Nr);
                   }
                    else
                    {
                    patientObj.Nr = 1;
                    Console.WriteLine("næstenr else : " + patientObj.Nr);
                    }
                }
                else
                {
                    patientObj.Nr = 1;
                    Console.WriteLine("næstenr else2 : " + patientObj.Nr);
                }
                DBConnection.Close();
                string myDato = "" + patientObj.IndDato.Year;
                if (patientObj.IndDato.Month >= 10)
                {
                    myDato = myDato + patientObj.IndDato.Month;
                }
                else
                {
                    myDato = myDato + "0" + patientObj.IndDato.Month;
                }

                if (patientObj.IndDato.Day >= 10)
                {
                    myDato = myDato + patientObj.IndDato.Day;
                }
                else
                {
                    myDato = myDato + "0" + patientObj.IndDato.Day;
                }

                Console.WriteLine("dato ok");

                string sql1 = " insert into patient (nr, navn,adresse, postnr, byen, telefon, alder,  dato,famorienteret, medicin, vaerdi, modul)" +
                       "values ( " + patientObj.Nr + ",'"
                        + patientObj.Navn + "','"
                        + patientObj.Adresse + "',"
                        + patientObj.Postnr + ",'"
                        + patientObj.Byen + "',"
                        + patientObj.Telefon + ","
                        + patientObj.Alder + ",'" 
                        + myDato   + "','"
                        + patientObj.Samtykke + "','"
                        + patientObj.Medicin + "','"
                        + patientObj.Vaerdi + "','"
                        + patientObj.Modul.Modulnr + "')";

                Console.WriteLine("Insert sql : " + sql1);
                dbCmd = DBConnection.GetDbCommand(sql1);

                rc = dbCmd.ExecuteNonQuery();
                DBConnection.Close(); 
               


            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert patient");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
    }
    }

